package com.power.project.service.impl;

import com.power.base.service.impl.BaseServiceImpl;
import com.power.project.dao.P_projectuserDao;
import com.power.project.model.P_projectuser;
import com.power.project.model.pageInfo.P_projectuserPageInfo;
import com.power.project.service.P_projectuserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 *  参与项目人员业务层实现
 */
@Service
public class P_projectuserServiceImpl extends BaseServiceImpl implements P_projectuserService {

    @Autowired
    private P_projectuserDao dao;

    @Override
    public P_projectuser getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(P_projectuser p_projectuser) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (p_projectuser.getPrimaryKey() != null) {
        	 P_projectuser p_projectuserfDb = dao.findById(p_projectuser.getPrimaryKey()).orElse(null);
             if(p_projectuserfDb != null) {
                BeanUtils.copyProperties(p_projectuser, p_projectuserfDb);
                dao.save(p_projectuserfDb);
                return;
             }
        }
        dao.save(p_projectuser);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<P_projectuser> findAll(P_projectuserPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<P_projectuser> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<P_projectuser> list) {
		dao.saveAll(list);
	}


    @Override
    public List<P_projectuser> findByProjectidAndWwuseridIn(Integer projectid, List<String> wwuserids) {
        return dao.findByProjectidAndWwuseridIn(projectid,wwuserids);
    }

    @Override
    public void deleteByProjectIdAndWwuserids(Integer projectId, List<String> userIds) {
        dao.deleteByProjectIdAndWwuserids(projectId,userIds);
    }

    @Override
    public List<P_projectuser> findByProjectidIn(List<Integer> pIdList) {
        return dao.findByProjectidIn(pIdList);
    }


    @Override
    public List<Integer> getProjectIdsByUserIds(List<String> wwuserids) {
        return dao.findDistinctProjectIdsByWwuseridIn(wwuserids);
    }

    @Override
    public List<String> getUseridsByProjectId(Integer projectId) {
        return dao.findWwuseridByProjectid(projectId);
    }
}
